<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/11
 * Time: 下午4:26
 */

namespace App\Admin\Controllers;

use App\Busi\Traits\UrlTagTrait;
use App\Models\ThirdUrl;
use App\Models\UrlTag;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;

class ThirdUrlController extends Controller
{
    use ModelForm,UrlTagTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('友情链接管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增友情链接');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑友情链接');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {

        return Admin::grid(ThirdUrl::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $obj = $this;
            $grid->tag_id('所属标签')->display(function ($tag_id) use($obj){
                return $obj->getTagNameByTypeId($tag_id);
            });
            $grid->title('标题');
            $grid->url('链接');
            $grid->show_order('展示顺序');
            $grid->flag('是否生效')->display(function ($flag){
                if($flag == 1) return "有效";
                return "无效";
            });


            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(ThirdUrl::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('tag_id', '关联标签')->options(UrlTag::pluck("name","id"));
            $form->text('title','标题');
            $form->text('url','链接');
            $form->text('show_order','显示顺序');
            $form->radio("flag","是否生效")->options(["0"=>"无效","1"=>"有效"])->default(1);


        });
    }
}